<template>
  <van-cell class="comment-item">
    <van-image slot="icon" class="avatar" round fit="cover"
      :src="common.aut_photo" />
    <div slot="title" class="title-wrap">
      <div class="user-name">{{common.aut_name}}</div>
      <van-button class="like-btn"
        :icon="common.is_liking?'good-job':'good-job-o'" :loading="loading"
        :class="{liked:common.is_liking}" @click="onLike">
        {{common.like_count||'赞'}}
      </van-button>
    </div>

    <div slot="label">
      <p class="comment-content">{{common.content}}</p>
      <div class="bottom-info">
        <span class="comment-pubdate">{{common.pubdate|relativeTime}}</span>
        <van-button class="reply-btn" round
          @click="$emit('reply-click',common)">回复 {{common.reply_count}}
        </van-button>
      </div>
    </div>
  </van-cell>
</template>

<script>
// 导入是否对评论点赞的Api
import { likeCommentsApi, cancelLikeCommentsApi } from '@/api'
export default {
  name: 'CommentItem',
  components: {},
  props: {
    common: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  computed: {},
  watch: {},
  created () { },
  mounted () { },
  methods: {
    // 对评论点赞的业务逻辑
    async onLike () {
      this.loading = true
      try {
        if (this.common.is_liking) {
          await cancelLikeCommentsApi(this.common.aut_id)
          this.common.like_count--
        } else {
          await likeCommentsApi(this.common.aut_id)
          this.common.like_count++
        }
        this.common.is_liking = !this.common.is_liking
        this.$toast.success(this.common.is_liking ? '点赞成功' : '取消点赞')
      } catch (err) {
        console.log(err)
        this.$toast.fail('请求失败')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang="less">
.comment-item {
  .avatar {
    width: 72px;
    height: 72px;
    margin-right: 25px;
  }
  .title-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .user-name {
      color: #406599;
      font-size: 26px;
    }
  }
  .comment-content {
    font-size: 32px;
    color: #222222;
    word-break: break-all;
    text-align: justify;
  }
  .comment-pubdate {
    font-size: 19px;
    color: #222;
    margin-right: 25px;
  }
  .bottom-info {
    display: flex;
    align-items: center;
  }
  .reply-btn {
    // width: 135px;
    height: 48px;
    line-height: 48px;
    font-size: 21px;
    color: #222;
  }
  .like-btn {
    height: 30px;
    padding: 0;
    border: none;
    font-size: 19px;
    line-height: 30px;
    margin-right: 7px;
    .van-icon {
      font-size: 30px;
    }
    &.liked {
      color: #e5645f;
    }
  }
}
</style>
